package com.autumn.zero.file.storage.application.services;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.autumn.common.api.domain.entities.IEntity;
import com.autumn.domain.repositories.IEntityRepository;
import com.autumn.mybatis.criterion.EntityQuery;
import com.autumn.zero.application.services.AbstractZeroEditAppService;
import com.autumn.zero.file.storage.services.vo.FileAttachmentInformationResponse;

/**
 * 文件上传编辑应服务抽象
 * 
 * @author 老码农 2019-05-11 14:06:11
 * @param <TEntity>
 *            实体类型
 * @param <TRepository>
 *            仓储类型
 * @param <TAddInput>
 *            添加输入类型
 * @param <TUpdateInput>
 *            更新输入类型
 * @param <TOutput>
 *            输出类型
 * @param <TQueryEntity>
 *            查询实体类型
 * @param <TQueryRepository>
 *            查询仓储类型
 * @param <TQueryItemResult>
 *            查询项目结果类型
 */
public abstract class AbstractZeroFileUploadEditAppService<TEntity extends IEntity<Long>, TRepository extends IEntityRepository<TEntity, Long>, TAddInput, TUpdateInput extends IEntity<Long>, TOutput, TQueryEntity extends IEntity<Long>, TQueryRepository extends IEntityRepository<TQueryEntity, Long>, TQueryItemResult>
		extends
		AbstractZeroEditAppService<Long, TEntity, TRepository, TAddInput, TUpdateInput, TOutput, TQueryEntity, TQueryRepository, TQueryItemResult>
		implements IFileUploadAppService {
	/**
	 * 文件上传管理
	 */
	@Autowired
	protected IFileUploadManager fileUploadManager;

	/**
	 * 
	 */
	public AbstractZeroFileUploadEditAppService() {
		super();
	}

	/**
	 * 
	 * @param entityClass
	 * @param qutputClass
	 * @param queryEntityClass
	 * @param queryItemResultClass
	 */
	protected AbstractZeroFileUploadEditAppService(Class<TEntity> entityClass, Class<TOutput> qutputClass,
			Class<TQueryEntity> queryEntityClass, Class<TQueryItemResult> queryItemResultClass) {
		super(entityClass, qutputClass, queryEntityClass, queryItemResultClass);
	}

	/**
	 * 
	 * @param entityClassArgumentsName
	 * @param qutputClassArgumentsName
	 * @param queryEntityClassArgumentsName
	 * @param queryItemResultClassArgumentsName
	 */
	protected AbstractZeroFileUploadEditAppService(String entityClassArgumentsName, String qutputClassArgumentsName,
			String queryEntityClassArgumentsName, String queryItemResultClassArgumentsName) {
		super(entityClassArgumentsName, qutputClassArgumentsName, queryEntityClassArgumentsName,
				queryItemResultClassArgumentsName);
	}

	/**
	 * 使用上传文件
	 * 
	 * @param id
	 * @param input
	 *            输入
	 * @return
	 */
	protected List<FileAttachmentInformationResponse> useUploadFiles(Long id, Object input) {
		return this.fileUploadManager.useUploadFiles(this, id, input);
	}

	@Override
	protected TOutput addAfter(TAddInput input, TEntity entity, EntityQuery<TEntity> query) {
		List<FileAttachmentInformationResponse> uploadFiles = this.fileUploadManager.useUploadFiles(this,
				entity.getId(), input);
		TOutput result = super.addAfter(input, entity, query);
		this.fileUploadManager.loadUploadFileOutput(uploadFiles, result);
		return result;
	}

	@Override
	protected TOutput updateAfter(TUpdateInput input, TEntity entity, TEntity oldEntity, EntityQuery<TEntity> query) {
		List<FileAttachmentInformationResponse> uploadFiles = this.fileUploadManager.useUploadFiles(this,
				entity.getId(), input);
		TOutput result = super.updateAfter(input, entity, oldEntity, query);
		this.fileUploadManager.loadUploadFileOutput(uploadFiles, result);
		return result;
	}

	@Override
	public TOutput queryById(Long id) {
		TOutput result = super.queryById(id);
		this.fileUploadManager.loadUploadFileOutput(this, id, result);
		return result;
	}

	@Override
	protected void deleteAfter(Long id, boolean isSoftDelete) {
		super.deleteAfter(id, isSoftDelete);
		if (!isSoftDelete) {
			this.fileUploadManager.deleteUploadFiles(this, id);
		}
	}

}
